﻿using Net_SCADA.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Net_SCADA
{
    public partial class Form_Login : fdBaseForm
    {
        //public int formLanguageIndex { get; set; } = -1;
        public Form_Login()
        {
            InitializeComponent();
        }

        private void Form_Login_Load(object sender, EventArgs e)
        {
            fdUsers.iniLoadUses();
            LoadUserInfo();
        }
        private void LoadUserInfo()
        {
            for (int i = 0; i < fdUsers.UsersName.Count; i++)
            {
                cmb_UsersName.Items.Add(fdUsers.UsersName[i].ToString());
            }
            if(cmb_UsersName.Items.Count>0) cmb_UsersName.SelectedIndex = 0;

        }
        private void _login()
        {
            if (fdUsers.iniCheckUser(new StringBuilder(cmb_UsersName.Text), new StringBuilder(txt_PWD.Text)))
            {   //  根据请求的index来判断权限的问题
                switch (fdUsers.LoginIndex)
                {
                    case 8://退出界面
                        netForms.showFormExit();
                        LoginCorrect();
                        break;
                    case 0:
                        LoginCorrect();
                        break;
                    case 1://工艺设计界面
                        if (fdUsers.Rights[cmb_UsersName.SelectedIndex] < 4)
                        {
                            fdMessage.ShowMsg(MultipleLanguage.messageInfos[13], "", formLanguageIndex);
                           // fdMessage.ShowMsg("当前的用户权限不够！请重新登陆", "登陆错误", 1, 1);
                        }
                        else
                        {
                            netForms.Formindex = 1;
                            LoginCorrect();
                        }
                        break;
                    case 2://工艺执行界面
                        if (fdUsers.Rights[cmb_UsersName.SelectedIndex] < 1)
                        {
                            fdMessage.ShowMsg(MultipleLanguage.messageInfos[13], "", formLanguageIndex);
                          //  fdMessage.ShowMsg("当前的用户权限不够！请重新登陆","登陆错误", 1, 1);
                        }
                        else
                        {
                            netForms.Formindex = 2;
                            netForms.fmRun.ShowCurrentPara();
                            LoginCorrect();
                        }
                        break;
                    case 4://系统设置界面
                        if (fdUsers.Rights[cmb_UsersName.SelectedIndex] < 4)
                        {
                            fdMessage.ShowMsg(MultipleLanguage.messageInfos[13], "", formLanguageIndex);
                           // fdMessage.ShowMsg("当前的用户权限不够！请重新登陆", "登陆错误", 1, 1);
                        }
                        else
                        {
                            netForms.Formindex = 4;
                            LoginCorrect();
                        }
                        break;
                    case 5:
                        if (fdUsers.Rights[cmb_UsersName.SelectedIndex] < 4)
                        {
                            fdMessage.ShowMsg(MultipleLanguage.messageInfos[13], "", formLanguageIndex);
                           // fdMessage.ShowMsg("当前的用户权限不够！请重新登陆" ,"登陆错误" , 1 , 1);
                        }
                        else
                        {
                            netForms.Formindex = 5;
                            LoginCorrect();
                        }
                        break;
                    case 6:
                        if (fdUsers.Rights[cmb_UsersName.SelectedIndex] < 4)
                        {
                            fdMessage.ShowMsg(MultipleLanguage.messageInfos[13], "", formLanguageIndex);
                            //fdMessage.ShowMsg("当前的用户权限不够！请重新登陆" , "登陆错误" , 1 , 1);
                        }
                        else
                        {
                            LoginCorrect();
                            netForms.fmPbox.StartModify();
                        }
                        break;
                }
            }
            else
            {
                lbl_Error.Visible = true;
            }
        }

        private void LoginCorrect()
        {
            fdUsers.UserRights = fdUsers.Rights[cmb_UsersName.SelectedIndex];
            fdUsers.UserName = new StringBuilder(cmb_UsersName.Text);
            netForms.fmMenu.MenuButton[7].Text = "用户注销";
            this.Dispose();
        }


        private void txt_PWD_Click(object sender, EventArgs e)
        {
            lbl_Error.Visible = false;
        }
        private void txt_PWD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return) _login();
        }

        private void Form_Login_Activated(object sender, EventArgs e)
        {
            //if (formLanguageIndex != MultipleLanguage.systemLanguageIndex) MultipleLanguage.switchLanguage(this);
            txt_PWD.Focus();
        }

        private void btn_Login_Click(object sender , EventArgs e)
        {
            _login();
        }

        private void btn_Cancel__Click(object sender , EventArgs e)
        {
            this.Dispose();
        }


    }
}
